package com.acoreful.bootsample.config;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.acoreful.bootsample.util.BooleanUtils;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

@Configuration
public class WebMvcConfig {
	private  final String DF_YYYYMMDD_HHMMSS="yyyy-MM-dd HH:mm:ss";
	
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer customJackson() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.serializationInclusion(Include.ALWAYS);
                jacksonObjectMapperBuilder.failOnUnknownProperties(false);
                jacksonObjectMapperBuilder.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
                DateFormat df=new SimpleDateFormat(DF_YYYYMMDD_HHMMSS);
                jacksonObjectMapperBuilder.dateFormat(df);
                jacksonObjectMapperBuilder.serializerByType(Boolean.class, new JsonSerializer<Object>() {
	            		public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException ,JsonProcessingException {
	            			gen.writeNumber(BooleanUtils.boolToInt((Boolean)value));
	            		};
                });
            }

        };
    }
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        
        /*
         * 返回的JSON字符串中含有我们并不需要的字段，那么当对应的实体类中不含有该字段时，会抛出一个异常，告诉你有些字段没有在实体类中找到
         */
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        
        SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
        
        // 允许单引号 
        //objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);  
        // 字段和值都加引号  
        //objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  
        // 数字也加引号  
        //objectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);  
        //objectMapper.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Boolean.class, new JsonSerializer<Object>() {
        		public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException ,JsonProcessingException {
        			gen.writeNumber(BooleanUtils.boolToInt((Boolean)value));
        		};
        } );
        objectMapper.registerModule(simpleModule);
        //Null值输出空字符串
        serializerProvider.setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            		
            		jsonGenerator.writeString("");
            }
            
        });
        return objectMapper;
    }

}
